package tools;
/*
Collections是集合工具类，用来操作集合的工具
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CollectionsDemo {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        //批量添加
        Collections.addAll(list, "A", "B", "C", "D", "E", "F");
        System.out.println(list);
        //二分查找
        System.out.println(Collections.binarySearch(list, "C"));
        //打乱
        Collections.shuffle(list);
        System.out.println(list);
        //找最大值，最小值，
        ArrayList<Integer> nums = new ArrayList<>();
        Collections.addAll(nums, 1, 2, 3, 4, 5);
        System.out.println(Collections.max(nums));
        System.out.println(Collections.min(nums));
        //若想找对象的最值，需要类继承Compareable接口，重写compareTo方法

        //对集合中的元素进行交换
        Collections.swap(list,0,1);
        System.out.println(list);
        //排序,默认升序，想降序用比较器
        ArrayList<Integer> nums1 = new ArrayList<>();
        Collections.addAll(nums1, 1, 2, 3, 4, 5);
        Collections.sort(nums1);
        System.out.println(nums1);
        Collections.sort(nums1,new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        System.out.println(nums1);
    }
}
